package com.orangelife.mobile.login.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bonree.agent.android.Bonree;
import com.common.sp.AppSp;
import com.common.sp.UserSp;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.SystemInfoUtil;
import com.curry.log.config.AppConfig;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.CommonJumper;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.guide.activity.GuideFragmentActivity;
import com.orangelife.mobile.main.biz.JumpUtil;
import com.orangelife.mobile.util.BLog;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 闪屏
 *
 * @author shz
 */
@Deprecated
public class SplashActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.tvVersion)
    TextView tvVersion;
    Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash2);

        ButterKnife.bind(this);
        initBonree();
        initBLog();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!AppSp.isFirstEnter()) {/* 由于默认只能返回false所以弄成相反的 */
                    IntentHelper.getInstances().getIntent(SplashActivity.this,
                            GuideFragmentActivity.class); /* 从启动动画ui跳转到主ui */
                    AppSp.setIsFirstEnter(true);
                    SplashActivity.this.finish(); /* 结束启动动画界面 */
                } else {
                    String scheme = SplashActivity.this.getIntent().getScheme();
                    String dataString = SplashActivity.this.getIntent().getDataString();
                    CommonJumper.jumpToMain(SplashActivity.this);
                    if (!StringUtil.isBlank(scheme) && !StringUtil.isBlank(dataString)) {
                        Uri uri = Uri.parse(dataString);
                        if (uri.getScheme().equals("orangelife") && uri.getHost().equals("www.com")) {
//                            ExplainUrlUtils.explainUrl(uri, SplashActivity.this, true);
                            JumpUtil.jumpActivity(SplashActivity.this, dataString, true);
                        }
                    }
                    SplashActivity.this.finish(); // 结束启动动画界面

                }
            }
        }, 3000); // 启动动画持续3秒钟*/
    }


    private void initBonree() {
        Bonree.withApplicationToken("8c59e556-e60e-45ac-800f-2b6f12397724").withLocationServiceEnabled(true).start(this);
    }

    /**
     * 接收处理线程返回的消息
     */
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 500:
                    HashMap<String, Object> map2 = (HashMap<String, Object>) JSONHelper
                            .jsonToMap(String.valueOf(msg.obj));
                    if (map2 != null) {
                        String errInfo2 = String.valueOf(map2
                                .get(Constant.RESULT_MESSAGE_KEY));
                        ToastHelper.getInstance().displayToastWithQuickClose(
                                errInfo2);
                    } else {
                        ToastHelper.getInstance().displayToastWithQuickClose(
                                msg.obj.toString());
                    }
                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                default:
                    break;
            }
        }

        ;
    };

    private void initBLog() {
        AppConfig appCongfig = new AppConfig();
        appCongfig.setContext(getApplicationContext());
        appCongfig.setMemberID(UserSp.getLoginID());
        BLog.getInstance().initBLog(appCongfig);
        tvVersion.setText("橙子生活 V " + SystemInfoUtil.getVersionName(this));
    }
}